package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.guoguojie.dao.AppEventDao;
import cn.sumpu.app.guoguojie.domain.AppEvent;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class AppEventDaoImpl extends SqlSessionDaoSupport implements AppEventDao {

	@Override
	public void insert(AppEvent appEvent) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.AppEventMapper.insert",
					appEvent);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public void update(AppEvent appEvent) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.AppEventMapper.update",
					appEvent);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update appEvent failure.", e);
			}
			throw new AppBusinessException("update appEvent failure.", e);
		}

	}

	@SuppressWarnings("unchecked")
	public List<AppEvent> getAppEvent(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.AppEventMapper.getAppEvent",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	
	

	@Override
	public int getAppEventCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.AppEventMapper.getAppEventCount",
							map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@Override
	public void deleteById(String identifier) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.AppEventMapper.deleteById",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteBlogById failure.", e);
			}
			throw new AppBusinessException("deleteBlogById failure.", e);
		}

	}

}
